<template>
    <view>
        <!--<u-navbar bgColor="#5c6be3" :placeholder="true">
            <view slot="left">
                <u-icon name="arrow-left" color="#fff" size="16" @click="onBackBtn"></u-icon>
            </view>
            <view slot="center" style="color: #fff;">络筒管理</view>
        </u-navbar>-->
        <u-sticky>
            <view class="lt-header">
                <view class="header-item" @click="onClickWorkshop">
                    <view class="item-gap">{{ searchParams.workshopName || '暂无车间' }}</view>
                    <u-icon name="arrow-down" size="16" color="#fff"></u-icon>
                </view>
                <view class="header-item" @click="onClickMachine">
                    <view class="item-gap">{{ searchParams.machineNameOrCode || '全部机台' }}</view>
                    <u-icon name="arrow-down" size="16" color="#fff"></u-icon>
                </view>
            </view>
        </u-sticky>
        <view class="legend-container">
            <view class="legend-item">
                <view class="legend-item-icon legend-item-open-icon"></view>
                <view>已开台</view>
            </view>
            <view class="legend-item">
                <view class="legend-item-icon"></view>
                <view>未开台</view>
            </view>
        </view>
        <view class="lt-container">
            <view class="lt-content">
                <view v-show="!tableList.length" class="text-center font-size-14">暂无数据</view>
                <u-checkbox-group
                    v-model="checkboxArr"
                    placement="column"
                    size="22"
                >

                    <view
                        v-for="(machineItem, machineIndex) in tableList"
                        :key="machineIndex"
                        class="machine-item"
                    >
                        <view class="machine-item-label">{{ machineItem.machineName }}</view>
                        <view class="machine-item-groups">
                            <view
                                v-for="(spinItem, spinIndex) in machineItem.spinGroupList"
                                :key="spinIndex"
                                class="spin-item"
                            >
                                <view
                                    class="spin-item-inner"
                                    :style="{
                                        borderColor: spinItem.productTaskList.some(x => x.openingState === 3) ? '#19be6b' : '',
                                        background: spinItem.productTaskList.some(x => x.openingState === 3) ? '#f6ffed' : ''
                                    }"
                                >
                                    <view class="spin-item-header" @click="onSpinGroupHeader(`${machineItem.machineId};${machineItem.machineCode};${machineItem.machineName};${spinItem.startSpinNumber};${spinItem.endSpinNumber}`, machineItem, spinItem)">
                                        <u-checkbox
                                            style="float: left;position: absolute;left: 0;"
                                            :customStyle="{marginBottom: '0px', float: 'left', position: 'absolute', left: '14rpx'}"
                                            label=" "
                                            :name="`${machineItem.machineId};${machineItem.machineCode};${machineItem.machineName};${spinItem.startSpinNumber};${spinItem.endSpinNumber}`"
                                        >
                                        </u-checkbox>
                                        {{ `${spinItem.startSpinNumber}~${spinItem.endSpinNumber}` }}
                                    </view>
                                    <view
                                        class="product-item"
                                        @click="onSelectMachineAndSpinItem(machineItem, spinItem)"
                                    >
                                        <view v-if="spinItem.productTaskList.find(x => x.openingState === 3)">
                                            <view style="color: #19be6b;">{{ spinItem.productTaskList.find(x => x.openingState === 3).productName }}</view>
                                        </view>
                                        <view v-else-if="spinItem.productTaskList.length >= 1">{{ spinItem.productTaskList[0].productName }}</view>
                                        <view v-else style="color: #c5c8ce;">无</view>
                                        <view class="text-center" style="color: #2b85e4;" v-show="spinItem.productTaskList.length > 1">
                                            <text>+{{ spinItem.productTaskList.length - 1 }}</text>
                                        </view>
                                    </view>
                                </view>
                            </view>
                        </view>
                    </view>
                </u-checkbox-group>
            </view>
            <view class="footer-operation-box">
                <u-button @click="onProductionBtn" class="push-button-style" size="large" text="排产" color="#5c6be3"></u-button>
            </view>
        </view>
        <select-product-modal
            :modalState="selectProductModalState"
            :productTaskList="selectSpinItem.productTaskList"
            @confirm="onConfirmOfSelectProduct"
            @on-visible-change="onVisibleChangeOfSelectProductModal"
        ></select-product-modal>
        <jk-popup :show="openPopupState" @close="openPopupState = false" :closeOnClickOverlay="false" confirmText="开台" :showCancelButton="true"  title="开台" >
            <view class="slot-content">
                <view class="lt-form-item">
                    <view class="lt-form-item-label">机台：</view>
                    <view class="lt-form-item-value">{{ ruleForm.machineName }}</view>
                </view>
                <view class="lt-form-item">
                    <view class="lt-form-item-label">锭组：</view>
                    <view class="lt-form-item-value">{{ `${ruleForm.startSpinNumber}~${ruleForm.endSpinNumber}` }}</view>
                </view>
                <view class="lt-form-item">
                    <view class="lt-form-item-label">品种：</view>
                    <view class="lt-form-item-value">{{ ruleForm.productName }}</view>
                </view>
                <view class="lt-form-item">
                    <view class="lt-form-item-label">批号：</view>
                    <view class="lt-form-item-value">{{ ruleForm.batchCode }}</view>
                </view>
                <view class="lt-form-item">
                    <view class="lt-form-item-label">纸管：</view>
                    <view class="lt-form-item-value">{{ ruleForm.paperTubeList ? ruleForm.paperTubeList.map(x => x.name).join(',') : '' }}</view>
                </view>
                <view class="lt-form-item">
                    <view class="lt-form-item-label">开台时间：</view>
                    <view @click="onSelectStartTime" class="lt-form-item-value">{{ ruleForm.startSystemTime }}</view>
                </view>
                <view class="lt-form-item">
                    <view class="lt-form-item-label">产量表数：</view>
                    <view class="lt-form-item-value">
                        <input
                            placeholder="请输入"
                            border
                            type="digit"
                            class="lt-form-item-ipt"
                            v-model="ruleForm.startOutput"
                        ></input>
                    </view>
                </view>
                <view class="lt-form-item">
                    <view class="lt-form-item-label">能耗表数：</view>
                    <view class="lt-form-item-value">
                        <input
                            placeholder="请输入"
                            border
                            type="digit"
                            class="lt-form-item-ipt"
                            v-model="ruleForm.startElectricEnergy"
                        ></input>
                    </view>
                </view>
                <u-button :loading="openBtnLoading" @click="onConfirmOpenBtn" type="primary" size="large">确认开台</u-button>
            </view>
        </jk-popup>
        <jk-popup :show="closePopupState" @close="closePopupState = false" :closeOnClickOverlay="false" confirmText="了机" :showCancelButton="true"  title="了机" >
            <view class="slot-content">
                <view>
                    <view class="lt-form-item">
                        <view class="lt-form-item-label">机台：</view>
                        <view class="lt-form-item-value">{{ `${ruleForm.machineName} (${ruleForm.startSpinNumber}~${ruleForm.endSpinNumber})` }}</view>
                    </view>
                    <view class="lt-form-item">
                        <view class="lt-form-item-label">品种：</view>
                        <view class="lt-form-item-value">{{ ruleForm.productName }}</view>
                    </view>
                    <view class="lt-form-item">
                        <view class="lt-form-item-label">批号：</view>
                        <view class="lt-form-item-value">{{ ruleForm.batchCode }}</view>
                    </view>
                    <view class="lt-form-item">
                        <view class="lt-form-item-label">纸管：</view>
                        <view class="lt-form-item-value">{{ ruleForm.paperTubeList ? ruleForm.paperTubeList.map(x => x.name).join(',') : '' }}</view>
                    </view>
                    <view class="lt-form-item">
                        <view class="lt-form-item-label">了机时间：</view>
                        <view @click="onSelectEndTime" class="lt-form-item-value edit-input">{{ ruleForm.endSystemTime }}</view>
                    </view>
                    <view class="lt-form-item">
                        <view class="lt-form-item-label">产量表数：</view>
                        <view class="lt-form-item-value">
                            <input
                                placeholder="请输入"
                                border
                                type="digit"
                                class="lt-form-item-ipt edit-input"
                                v-model="ruleForm.endOutput"
                            ></input>
                        </view>
                    </view>
                    <view class="lt-form-item">
                        <view class="lt-form-item-label">能耗表数：</view>
                        <view class="lt-form-item-value">
                            <input
                                placeholder="请输入"
                                border
                                type="digit"
                                class="lt-form-item-ipt edit-input"
                                v-model="ruleForm.endElectricEnergy"
                            ></input>
                        </view>
                    </view>
                    <view class="">
                        <u-button :loading="closeBtnLoading" @click="onConfirmCloseBtn" type="primary" size="large">确认了机</u-button>
                    </view>
                </view>
            </view>
        </jk-popup>
        <u-datetime-picker
            v-model="selectDateTime"
            :show="selectDateTimePickerState"
            :closeOnClickOverlay="true"
            mode="datetime"
            @confirm="onConfirmBtnOfSelectPicker"
            @close="onCloseBtnOfSelectPicker(false)"
            @cancel="onCloseBtnOfSelectPicker(false)"
        ></u-datetime-picker>
        <select-operation-type-modal
            :modalState="selectOperationTypeModalState"
            @confirm="onConfirmOfSelectOperationTypeModalState"
            @on-visible-change="onVisibleChangeOfSelectOperationTypeModalState"
        ></select-operation-type-modal>
        <jk-popup :show="showSelectMachinePopup" :closeOnClickOverlay="true" title="选择机台" @close="onSelectMachinePopup">
            <u-radio-group v-model="selectMachineVal" size="21" iconPlacement="right" placement="column" :borderBottom="true">
                <view style="max-height: 400px;overflow: auto;">
                    <u-radio
                        v-for="item in machineList"
                        :key="item.id"
                        labelSize="18"
                        iconSize="24"
                        size="24"
                        labelColor="#17233d"
                        :borderBottom="true"
                        :customStyle="{
                            margin: '8rpx',
                            height: '42px'
                        }"
                        :label="item.code"
                        :name="item.id"
                        style="margin: 10rpx;"
                    ></u-radio>
                </view>
                <u-button @click="onConfirmSelectMachineBtn" type="primary" size="large">确认</u-button>
            </u-radio-group>
        </jk-popup>
    </view>
</template>

<script>
import {
    productionPrdNoticeLtMachineChangeClearOpening,
    productionPrdNoticeLtMachineChangeClose,
    productionPrdNoticeLtMachineChangeGetMachineList, productionPrdNoticeLtMachineChangeOpening,
    productionPrdNoticeMachineChangeGetMachineData,
    systemAuthGetPermissionInfo, workforcemanagementScheduleShiftByTime
} from "../../../../api";
import {formatTime, showLoading} from "../../../../utils/jk-utils";
    import jkPopup from '../../../../jk-components/jk-popup/index';
    import selectProductModal from './components/select-product-modal';
    import selectOperationTypeModal from './components/select-operation-type-modal';
    export default {
        components: {
            selectOperationTypeModal,
            selectProductModal,
            jkPopup
        },
        data() {
            return {
                selectMachineVal: null,
                showSelectMachinePopup: false,
                closePopupState: false,
                selectOperationTypeModalState: false,
                selectDateTimePickerState: false,
                selectDateTime: '',
                openBtnLoading: false,
                closeBtnLoading: false,
                openPopupState: false,
                selectProduct: null,
                selectProductModalState: false,
                checkboxArr: [],
                searchParams: {
                    workshopId: null,
                    machineNameOrCode: ""
                },
                workshopList: [],
                machineList: [
                    { id: '-1', code: '全部机台' },
                    { id: 'LT001', code: 'LT001' },
                    { id: 'LT002', code: 'LT002' },
                    { id: 'LT003', code: 'LT003' },
                    { id: 'LT004', code: 'LT004' },
                    { id: 'LT005', code: 'LT005' },
                    { id: 'LT006', code: 'LT006' },
                ],
                tableList: [],
                selectSpinItem: {},
                selectMachineItem: {},
                productTaskItem: {},
                ruleForm: {}
            };
        },
        methods: {
            onBackBtn() {
                uni.switchTab({
                    url: '/pages/index/index'
                });
            },
            onConfirmSelectMachineBtn() {
                this.$set(this.searchParams, 'machineNameOrCode', this.selectMachineVal === '-1' ? '' : this.selectMachineVal);
                this.showSelectMachinePopup = false;
                this.getListRequest();
            },
            onSelectMachinePopup() {
                this.selectMachineVal = null;
                this.showSelectMachinePopup = false;
            },
            onConfirmOfSelectOperationTypeModalState(e) {
                if (e === 2) {
                    // 选择了机
                    console.log('了机', e)
                    this.$set(this.ruleForm, 'endSystemTime', formatTime(Date.now()));
                    this.getScheduleShiftForTimeRequest();
                    this.getGatherMachineDataRequest();
                    this.closePopupState = true;

                } else if (e === 1) {
                    // 选择取消开台
                    productionPrdNoticeLtMachineChangeClearOpening(
                        {},
                        {prdNoticeMachineChangeId: this.ruleForm.prdNoticeMachineChangeId},
                    ).then(res => {
                        if (res.data.status === 200) {
                            uni.showToast({
                                title: '操作成功'
                            });
                            this.getListRequest();
                        }
                    })

                }
                this.selectOperationTypeModalState = false;
            },
            onVisibleChangeOfSelectOperationTypeModalState(e) {
                this.selectOperationTypeModalState = e;
            },
            moveHandle() {
                return true;
                if (this.openPopupState || this.selectProductModalState) {
                    return true;
                } else {
                    return false;
                }
            },
            onSelectStartTime() {
                this.selectDateTimePickerState = true;
            },
            onSelectEndTime() {
                this.selectDateTimePickerState = true;
            },
            onConfirmBtnOfSelectPicker(e) {
                let timeKeyName = this.isOpen ? 'startSystemTime' : 'endSystemTime';
                this.$set(this.ruleForm, timeKeyName, formatTime(e.value));
                this.selectDateTimePickerState = false;
                this.getGatherMachineDataRequest();
                this.getScheduleShiftForTimeRequest();
            },
            onCloseBtnOfSelectPicker(e) {
                this.selectDateTimePickerState = e;
            },
            onConfirmCloseBtn() {
                this.closeSaveRequest();
            },
            closeSaveRequest() {
                this.closeBtnLoading = true;
                const params = {
                    'startSpinNumber': this.ruleForm.startSpinNumber,
                    'endSpinNumber': this.ruleForm.endSpinNumber,
                    'lastEndElectricEnergy': this.ruleForm.lastEndElectricEnergy,
                    'lastEndOutput': this.ruleForm.lastEndOutput,
                    'openSpinCount': this.ruleForm.openSpinCount,
                    'prdNoticeMachineChangeId': this.ruleForm.prdNoticeMachineChangeId,
                    'endElectricEnergy': this.ruleForm.endElectricEnergy,
                    'endOutput': this.ruleForm.endOutput,
                    'endScheduleShiftId': this.ruleForm.endScheduleShiftId,
                    'endSystemElectricEnergy': this.ruleForm.endSystemElectricEnergy,
                    'endSystemOutput': this.ruleForm.endSystemOutput,
                    'endSystemTime': this.ruleForm.endSystemTime
                };
                productionPrdNoticeLtMachineChangeClose(params).then(res => {
                    this.closeBtnLoading = false;
                    if (res.data.status === 200) {
                        uni.showToast({
                            title: '操作成功'
                        });
                        this.closePopupState = false;
                        this.getListRequest();
                    }
                });
            },
            onConfirmOpenBtn() {
                this.openSaveRequest();
            },
            openSaveRequest() {
                this.openBtnLoading = true;
                const params = {
                    'endSpinNumber': this.ruleForm.endSpinNumber,
                    'startSpinNumber': this.ruleForm.startSpinNumber,
                    'lastEndElectricEnergy': this.ruleForm.lastEndElectricEnergy,
                    'lastEndOutput': this.ruleForm.lastEndOutput,
                    'openSpinCount': this.ruleForm.openSpinCount,
                    'prdNoticeMachineChangeId': this.ruleForm.prdNoticeMachineChangeId,
                    'startElectricEnergy': this.ruleForm.startElectricEnergy,
                    'startOutput': this.ruleForm.startOutput,
                    'startScheduleShiftId': this.ruleForm.startScheduleShiftId,
                    'startSystemElectricEnergy': this.ruleForm.startSystemElectricEnergy,
                    'startSystemOutput': this.ruleForm.startSystemOutput,
                    'startSystemTime': this.ruleForm.startSystemTime
                };
                productionPrdNoticeLtMachineChangeOpening(params).then(res => {
                    this.openBtnLoading = false;
                    if (res.data.status === 200) {
                        uni.showToast({
                            title: '操作成功'
                        });
                        this.openPopupState = false;
                        this.getListRequest();
                    }
                });
            },
            onVisibleChangeOfSelectProductModal(e) {
                this.selectProductModalState = e;
            },
            onConfirmOfSelectProduct(e) {
                if (Object.keys(e).length) {
                    this.$set(this.ruleForm, 'prdNoticeMachineChangeId', e.id);
                    this.$set(this.ruleForm, 'productId', e.productId);
                    this.$set(this.ruleForm, 'productCode', e.productCode);
                    this.$set(this.ruleForm, 'productName', e.productName);
                    this.$set(this.ruleForm, 'batchId', e.batchId);
                    this.$set(this.ruleForm, 'batchCode', e.batchCode);
                    this.$set(this.ruleForm, 'paperTubeList', e.paperTubeList);
                    this.$set(this.ruleForm, 'spinGroupId', e.spinGroupId);
                    this.selectProductModalState = false;
                    this.openPopupState = true;
                    this.getGatherMachineDataRequest();
                } else {
                    this.selectProductModalState = false;
                    uni.showToast({
                        icon: 'error',
                        title: '请选择品种'
                    });
                }
            },
            // 获取指定机台采集的仪表数据
            getGatherMachineDataRequest() {
                let timeKeyName = this.isOpen ? 'startSystemTime' : 'endSystemTime';
                if (this.ruleForm.machineId && this.ruleForm.startSpinNumber) {
                    return productionPrdNoticeMachineChangeGetMachineData({
                        machineId: this.ruleForm.machineId,
                        machineCode: this.ruleForm.machineCode,
                        selectDateTime: this.ruleForm[timeKeyName],
                        startSpinNumber: this.ruleForm.startSpinNumber,
                        spinGroupId: this.ruleForm.spinGroupId
                    }).then(res => {
                        if (res.data.status === 200) {
                            if (this.isOpen) {
                                // 开台能耗表数，默认等于采集到的实时能耗
                                this.$set(this.ruleForm, 'startElectricEnergy', res.data.res.currentElectricEnergy || null);
                                this.$set(this.ruleForm, 'startSystemElectricEnergy', res.data.res.currentElectricEnergy || null);
                                // 开台产量表数，默认等于采集到的实时产量
                                this.$set(this.ruleForm, 'startOutput', res.data.res.currentOutput || null);
                                this.$set(this.ruleForm, 'startSystemOutput', res.data.res.currentOutput || null);
                                // 上次的产量和能耗
                                this.$set(this.ruleForm, 'lastEndOutput', res.data.res.lastEndOutput || null);
                                this.$set(this.ruleForm, 'lastEndElectricEnergy', res.data.res.lastEndElectricEnergy || null);

                                this.$set(this.ruleForm, 'openSpinCount', res.data.res.spinCount || null);
                            } else {
                                // 了机能耗表数，默认等于采集到的实时能耗
                                this.$set(this.ruleForm, 'endElectricEnergy', res.data.res.currentElectricEnergy || null);
                                this.$set(this.ruleForm, 'endSystemElectricEnergy', res.data.res.currentElectricEnergy || null);
                                // 了机产量表数，默认等于采集到的实时产量
                                this.$set(this.ruleForm, 'endOutput', res.data.res.currentOutput || null);
                                this.$set(this.ruleForm, 'endSystemOutput', res.data.res.currentOutput || null);
                                // 上次的产量和能耗
                                this.$set(this.ruleForm, 'lastEndOutput', res.data.res.lastEndOutput || null);
                                this.$set(this.ruleForm, 'lastEndElectricEnergy', res.data.res.lastEndElectricEnergy || null);

                                this.$set(this.ruleForm, 'openSpinCount', res.data.res.spinCount || null);
                            }
                        }
                    });
                }
            },
            getScheduleShiftForTimeRequest() {
                let timeKeyName = this.isOpen ? 'startSystemTime' : 'endSystemTime';
                if (this.ruleForm.workshopId && this.ruleForm[timeKeyName]) {
                    return workforcemanagementScheduleShiftByTime({
                        startTime: this.ruleForm[timeKeyName],
                        workshopId: this.ruleForm.workshopId
                    }).then(res => {
                        if (res.data.status === 200) {
                            let resData = res.data.res;
                            if (resData) {
                                if (this.isOpen) {
                                    this.$set(this.ruleForm, 'startScheduleShiftId', resData.shiftId);
                                    this.$set(this.ruleForm, 'startScheduleShiftName', resData.shiftName);
                                } else {
                                    this.$set(this.ruleForm, 'endScheduleShiftId', resData.shiftId);
                                    this.$set(this.ruleForm, 'endScheduleShiftName', resData.shiftName);
                                }
                            } else {
                                if (this.isOpen) {
                                    this.$set(this.ruleForm, 'startScheduleShiftId', null);
                                    this.$set(this.ruleForm, 'startScheduleShiftName', null);
                                } else {
                                    this.$set(this.ruleForm, 'endScheduleShiftId', null);
                                    this.$set(this.ruleForm, 'endScheduleShiftName', null);
                                }
                            }
                        }
                    });
                }
            },
            onSelectMachineAndSpinItem(machineItem, spinItem) {
                if (!spinItem.productTaskList.length) {
                    uni.showToast({
                        title: '请先排产',
                        icon: 'error'
                    });
                    return;
                }
                this.selectMachineItem = machineItem;
                this.selectSpinItem = spinItem;
                this.$set(this.ruleForm, 'workshopId', this.searchParams.workshopId);
                this.$set(this.ruleForm, 'workshopCode', this.searchParams.workshopCode);
                this.$set(this.ruleForm, 'workshopName', this.searchParams.workshopName);
                this.$set(this.ruleForm, 'machineId', machineItem.machineId);
                this.$set(this.ruleForm, 'machineCode', machineItem.machineCode);
                this.$set(this.ruleForm, 'machineName', machineItem.machineName);
                this.$set(this.ruleForm, 'startSpinNumber', spinItem.startSpinNumber);
                this.$set(this.ruleForm, 'endSpinNumber', spinItem.endSpinNumber);

                let openProductItem = spinItem.productTaskList.find(x => x.openingState === 3) || {};
                if (Object.keys(openProductItem).length) {
                    this.$set(this.ruleForm, 'prdNoticeMachineChangeId', openProductItem.id);
                    this.$set(this.ruleForm, 'productId', openProductItem.productId);
                    this.$set(this.ruleForm, 'productCode', openProductItem.productCode);
                    this.$set(this.ruleForm, 'productName', openProductItem.productName);
                    this.$set(this.ruleForm, 'batchId', openProductItem.batchId);
                    this.$set(this.ruleForm, 'batchCode', openProductItem.batchCode);
                    this.$set(this.ruleForm, 'paperTubeList', openProductItem.paperTubeList);
                    this.$set(this.ruleForm, 'spinGroupId', openProductItem.spinGroupId);
                    this.isOpen = false;
                    this.selectOperationTypeModalState = true;
                } else {
                    this.isOpen = true;
                    this.$set(this.ruleForm, 'startSystemTime', formatTime(Date.now()));
                    this.selectProductModalState = true;
                    this.getScheduleShiftForTimeRequest();
                }
            },
            onProductionBtn() {
                let item
                let arr = this.checkboxArr.map(x => {
                    item = x.split(';');
                    return {
                        machineId: item[0],
                        machineCode: item[1],
                        machineName: item[2],
                        startSpinNumber: item[3],
                        endSpinNumber: item[4],
                    };
                });
                getApp().globalData.ltSelectMachineGroup = arr;
                if (arr.length) {
                    wx.navigateTo({
                        url: '/pages/production/ltPrd/selectProductList/index'
                    });
                } else {
                    uni.showToast({
                        icon: 'error',
                        title: '请选择锭组'
                    });
                }
            },
            onSpinGroupHeader(e, machineItem, spinItem) {
                if (!this.checkboxArr.includes(e)) {
                    this.checkboxArr.push(e);
                } else {
                    this.checkboxArr.forEach((item, index) => {
                        if (item === e) {
                            this.checkboxArr.splice(index, 1);
                        }
                    })
                }
            },
            onClickMachine() {
                this.showSelectMachinePopup = true;
            },
            onClickWorkshop() {
                let _this = this;
                uni.showActionSheet({
                    itemList: _this.workshopList.map(x => x.workshopName),
                    success(res) {
                        _this.checkboxArr = [];
                        _this.$set(_this.searchParams, 'workshopId', _this.workshopList[res.tapIndex].workshopId);
                        _this.$set(_this.searchParams, 'workshopName', _this.workshopList[res.tapIndex].workshopName);
                        _this.getListRequest();
                    }
                });
            },
            groupByMachine(data) {
                let arr = [];
                let item = {};
                data.forEach(x => {
                    if (!item[x.machineCode]) {
                        item[x.machineCode] = x.machineCode;
                        arr.push({
                            machineId: x.machineId,
                            machineCode: x.machineCode,
                            machineName: x.machineName,
                            spinGroupList: [{
                                startSpinNumber: x.startSpinNumber,
                                endSpinNumber: x.endSpinNumber,
                                productTaskList: x.productTaskList
                            }]

                        });
                    } else {
                        arr.forEach(k => {
                            if (k.machineCode === x.machineCode) {
                                k.spinGroupList.push({
                                    startSpinNumber: x.startSpinNumber,
                                    endSpinNumber: x.endSpinNumber,
                                    productTaskList: x.productTaskList
                                })
                            }
                        })
                    }
                })
                this.tableList = JSON.parse(JSON.stringify(arr));
            },
            getListRequest() {
                if (!this.searchParams.workshopId) return false;
                showLoading(true);
                return productionPrdNoticeLtMachineChangeGetMachineList(this.searchParams).then(res => {
                    showLoading(false);
                    if (res.data.status === 200) {
                        this.tableList = res.data.res || [];
                        this.groupByMachine(res.data.res || []);
                    }
                })
            },
            getWorkshopDataRequest() {
                return systemAuthGetPermissionInfo().then(res => {
                    if (res.data.status === 200) {
                        if (!this.searchParams.workshopId) {
                            this.$set(this.searchParams, 'workshopId', res.data.res.workshop.defaultWorkshopId);
                            this.$set(this.searchParams, 'workshopCode', res.data.res.workshop.defaultWorkshopCode);
                            this.$set(this.searchParams, 'workshopName', res.data.res.workshop.defaultWorkshopName);
                        }
                        this.workshopList = res.data.res.workshop.workshops;
                    }
                })
            },
            async getDependentData() {
                showLoading(true);
                await this.getWorkshopDataRequest();
                await this.getListRequest();
            }
        },
        onShow() {
            this.selectDateTime = formatTime(Date.now());
            this.getDependentData();
        }
    }
</script>
<style scoped lang="scss">
    @import "./list";
</style>
